﻿using Dapper;
using Management.Systeam.Domaina;
using Management.Systeam.ErrorCode;
using Management.Systeam.Infrastructure.Implement;
using Management.Systeam.Infrastructure.Interface;
using Management.Systeam.Read.Apis.Application.Command.LedgerManagements;
using Management.Systeam.Read.Apis.Auto.UserDto;
using MediatR;
using System.Text;
using Warehouse.Domian.Model;

namespace Management.Systeam.Read.Apis.Application.CommandHandler.LedgerManagements
{
    public class ShowLedStockingCommandHandler : IRequestHandler<ShowLedStockingCommand, ApiPeaging<ApiResult<List<LedStoDto>>>>
    {
        IBaseRepository<LedStoDto> LedgerManagement;
        public DatabaseContext db;

        public ShowLedStockingCommandHandler(IBaseRepository<LedStoDto> LedgerManagement, DatabaseContext db)
        {
            this.LedgerManagement = LedgerManagement;
            this.db = db;
        }

        public async Task<ApiPeaging<ApiResult<List<LedStoDto>>>> Handle(ShowLedStockingCommand request, CancellationToken cancellationToken)
        {
            StringBuilder countSql = new StringBuilder();
            StringBuilder sb = new StringBuilder();
            sb.Append(@"
                SELECT
          a.LedgerInfoManagement_Id,
         a.Product_Code,
         a.Product_Name,
         a.Product_Status,
         a.Product_Type,
         a.Product_RuleNumber,
         a.Weight,
         a.NumberData,
         a.Warehouse_Name,
         a.Please_Name,        
         a.Quantity,        
         a.Money,        
         a.Wating_Into,        
         a.Wating_Out,        
         a.Storage_Unit,        
         a.Storage_Money,        
         a.Start_Unit,        
         a.End_Unit,         
         b.Id,
         b.ProductId,
         b.StockId,
         b.ProductCode,
         b.ProductName,
         b.ProductStatus,
         b.CategoryId,
         b.Specifications,
         b.UnitId,
         b.Barcode,
         b.DepotId,
         b.LocationId,
         b.Stocknum,
         b.StockMoney
                FROM
	                ledgerManagement a
	              JOIN stockModel b ON a.ledgerInfoManagement_Id = b.Id
                ");
            countSql.Append(@"SELECT
                  COUNT(a.LedgerInfoManagement_Id)
                       FROM
	                ledgerManagement a
	              JOIN stockModel b ON a.ledgerInfoManagement_Id = b.Id");

            //if (request.User_Name != null)
            //{
            //    sb.Append(" AND a.User_Name LIKE '%" + request.User_Name + "%'");
            //    countSql.Append(" AND a.User_Name LIKE '%" + request.User_Name + "%'");
            //}
            //if (request.User_WorkCode != null)
            //{
            //    sb.Append(" AND a.User_WorkCode LIKE '%" + request.User_WorkCode + "%'");
            //    countSql.Append(" AND a.User_WorkCode LIKE '%" + request.User_WorkCode + "%'");
            //}
            //if (request.Department_Id != null)
            //{
            //    sb.Append(" AND a.Department_Id LIKE '%" + request.Department_Id + "%'");
            //    countSql.Append(" AND a.Department_Id LIKE '%" + request.Department_Id + "%'");
            //}
            //if (request.Role_Id != null)
            //{
            //    sb.Append(" AND a.Role_Id LIKE '%" + request.Role_Id + "%'");
            //    countSql.Append(" AND a.Role_Id LIKE '%" + request.Role_Id + "%'");
            //}
            // 添加排序和分页
            sb.Append(" ORDER BY a.ledgerInfoManagement_Id DESC LIMIT @PageSize OFFSET @offset");

            // 设置参数
            var parame = new
            {
                //User_Name = request.User_Name,
                //User_WorkCode = request.User_WorkCode,
                //Department_Id = request.Department_Id,
                //Role_Id = request.Role_Id,
                PageSize = request.PageSize,
                offset = (request.PageIndex - 1) * request.PageSize
            };

            // 执行查询
            var pageData = await db.DapperConnection.QueryAsync<LedStoDto>(sb.ToString(), parame);
            int total = await db.DapperConnection.ExecuteScalarAsync<int>(countSql.ToString(), parame);
            int totalPage = (int)Math.Ceiling(total / (double)request.PageSize);
            return new ApiPeaging<ApiResult<List<LedStoDto>>>
            {
                Total = total,
                TotalPage = totalPage,
                Data = new ApiResult<List<LedStoDto>>
                {
                    Code = ApiEnum.success,
                    Message = "查询成功",
                    Data = pageData.ToList()
                }
            };
        }
        
    }
}
